package com.lframework.xingyun.basedata.impl.coupon;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.toolkit.JoinWrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import java.io.Serializable;

import com.lframework.starter.common.utils.Assert;
import com.lframework.xingyun.basedata.bo.coupon.CouponProductSelectorBo;
import com.lframework.xingyun.basedata.bo.coupon.QueryCouponProductBo;
import com.lframework.xingyun.basedata.entity.CouponProduct;
import com.lframework.xingyun.basedata.entity.Product;
import com.lframework.xingyun.basedata.entity.ProductBrand;
import com.lframework.xingyun.basedata.entity.ProductCategory;
import com.lframework.xingyun.basedata.mappers.CouponProductMapper;
import com.lframework.xingyun.basedata.service.coupon.CouponProductService;
import com.lframework.xingyun.basedata.vo.coupon.QueryCouponProductVo;
import com.lframework.xingyun.basedata.vo.coupon.QueryCouponProductSelectorVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CouponProductServiceImpl extends BaseMpServiceImpl<CouponProductMapper, CouponProduct> implements CouponProductService {

    @Override
    public List<CouponProductSelectorBo> querySelectProduct(Integer pageIndex, Integer pageSize, QueryCouponProductSelectorVo vo) {
        return JoinWrappers.lambda(Product.class)
                .selectAll(Product.class)
                .selectAs(ProductCategory::getId, CouponProductSelectorBo::getCategoryId)
                .selectAs(ProductCategory::getName, CouponProductSelectorBo::getCategoryName)
                .selectAs(ProductBrand::getId, CouponProductSelectorBo::getBrandId)
                .selectAs(ProductBrand::getName, CouponProductSelectorBo::getBrandName)
                .leftJoin(ProductCategory.class, ProductCategory::getId, Product::getCategoryId)
                .leftJoin(ProductBrand.class, ProductBrand::getId, Product::getBrandId)
                .and(StrUtil.isNotBlank(vo.getProductKeyword()), w -> {
                    w.like(Product::getName, vo.getProductKeyword())
                            .or()
                            .like(Product::getCode, vo.getProductKeyword())
                            .or()
                            .like(Product::getSkuCode, vo.getProductKeyword())
                            .or()
                            .like(ProductBrand::getShortName, vo.getProductKeyword());
                })
                .list(CouponProductSelectorBo.class);
    }

    @Override
    public PageResult<QueryCouponProductBo> query(Integer pageIndex, Integer pageSize, QueryCouponProductVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<QueryCouponProductBo> page = this.baseMapper.queryPage(vo);

        return PageResultUtil.convert(page);
    }

    @Override
    public List<QueryCouponProductBo> queryList(QueryCouponProductVo vo) {

        return getBaseMapper().queryList(vo);
    }

    @Cacheable(value = CouponProduct.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public CouponProduct findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @CacheEvict(value = CouponProduct.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

